package com.shanxi.cz.module.system.dal.mysql.logger;

import cn.idev.excel.util.StringUtils;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.shanxi.cz.framework.common.pojo.PageResult;
import com.shanxi.cz.framework.mybatis.core.mapper.BaseMapperX;
import com.shanxi.cz.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.shanxi.cz.module.system.api.logger.dto.OperateLogPageReqDTO;
import com.shanxi.cz.module.system.controller.admin.logger.vo.operatelog.OperateLogPageReqVO;
import com.shanxi.cz.module.system.dal.dataobject.logger.OperateLogDO;
import com.shanxi.cz.module.system.dal.dataobject.logger.OperateLogDTO;
import com.shanxi.cz.module.system.dal.dataobject.user.AdminUserDO;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface OperateLogMapper extends BaseMapperX<OperateLogDO> {

    default PageResult<OperateLogDTO> selectPage(OperateLogPageReqVO pageReqDTO) {
        MPJLambdaWrapper<OperateLogDO> wrapperX = JoinWrappers.lambda(OperateLogDO.class)
                .selectAssociation(AdminUserDO.class, OperateLogDTO::getUser)// 一对一
                .leftJoin(AdminUserDO.class, AdminUserDO::getId, OperateLogDO::getUserId)
                .eqIfExists(OperateLogDO::getUserId, pageReqDTO.getUserId())
                .eqIfExists(OperateLogDO::getBizId, pageReqDTO.getBizId())
                .likeIfExists(OperateLogDO::getType, pageReqDTO.getType())
                .likeIfExists(OperateLogDO::getSubType, pageReqDTO.getSubType())
                .likeIfExists(OperateLogDO::getAction, pageReqDTO.getAction())
                .likeIfExists(AdminUserDO::getNickname, pageReqDTO.getUserName())
                .orderByDesc(OperateLogDO::getId);
        if (StringUtils.isNotBlank(pageReqDTO.getCreateTimeFrom()) && StringUtils.isNotBlank(pageReqDTO.getCreateTimeTo())) {
            wrapperX.between(OperateLogDO::getCreateTime, pageReqDTO.getCreateTimeFrom(), pageReqDTO.getCreateTimeTo());
        }
        return selectJoinPage(pageReqDTO, OperateLogDTO.class, wrapperX);
    }

    default PageResult<OperateLogDO> selectPage(OperateLogPageReqDTO pageReqDTO) {
        return selectPage(pageReqDTO, new LambdaQueryWrapperX<OperateLogDO>()
                .eqIfPresent(OperateLogDO::getType, pageReqDTO.getType())
                .eqIfPresent(OperateLogDO::getBizId, pageReqDTO.getBizId())
                .eqIfPresent(OperateLogDO::getUserId, pageReqDTO.getUserId())
                .orderByDesc(OperateLogDO::getId));
    }

}
